`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2025/01/28 15:15:35
// Design Name: 
// Module Name: diff_time
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////

module disparity_time(
	input				clk,
	input				rst,
    input [5:0] 		sec,       // 绉�
    input [5:0] 		min,       // 鍒嗛挓
    input [5:0] 		hour,      // 灏忔椂
    input [5:0] 		next_sec,       // 绉�
    input [5:0] 		next_min,       // 鍒嗛挓
    input [5:0] 		next_hour,      // 灏忔椂	
	output reg 	[7:0]	diff_hour1      ,
	output reg 	[7:0]	diff_hour0      ,
	output reg 	[7:0]	diff_min1       ,
	output reg 	[7:0]	diff_min0       ,
	output reg 	[7:0]	diff_sec1       ,
	output reg 	[7:0]	diff_sec0		,
    output reg  [5:0] 	diff_hour		,    	// 璁＄畻鏃堕棿宸細灏忔椂
    output reg  [5:0] 	diff_min 		,    	// 璁＄畻鏃堕棿宸細鍒嗛挓
    output reg  [5:0] 	diff_sec 		   		// 璁＄畻鏃堕棿宸細绉� 
    );
	
    reg 		borrow_min;         // 鏄惁闇€瑕佸€熶綅锛堝垎閽燂級
    reg 		borrow_hour;        // 鏄惁闇€瑕佸€熶綅锛堝皬鏃讹級
    // reg [5:0] 	diff_hour;    		// 璁＄畻鏃堕棿宸細灏忔椂
    // reg [5:0] 	diff_min ;    		// 璁＄畻鏃堕棿宸細鍒嗛挓
    // reg [5:0] 	diff_sec ;    		// 璁＄畻鏃堕棿宸細绉� 

    // 璁＄畻 next_鏃堕棿 鍜� 褰撳墠鏃堕棿鐨勫樊鍊�
    always @(posedge clk or negedge rst) begin
        if (!rst) begin
            diff_hour <= 1;diff_min <= 2;diff_sec <= 3;
            borrow_min <= 0;borrow_hour <= 0;
        end else begin
            // 绉掑樊鐨勮绠�
            if (next_sec < sec) begin
                diff_sec <= 60 + next_sec - sec;  // 鍊熶綅
                borrow_min <= 1;                  // 闇€瑕佷粠鍒嗛挓鍊熶綅
            end else begin
                diff_sec <= next_sec - sec;
                borrow_min <= 0;  // 涓嶉渶瑕佸€熶綅
            end

            // 鍒嗛挓宸殑璁＄畻
            if (next_min < min) begin//涓€瀹氶渶瑕佸€熶綅
				if(borrow_min)begin
					diff_min <= 59 + next_min - min;  // 鍊熶綅
					borrow_hour <= 1;  // 闇€瑕佷粠灏忔椂鍊熶綅
				end else begin
					diff_min <= 60 + next_min - min;  // 鍊熶綅
					borrow_hour <= 1;  // 闇€瑕佷粠灏忔椂鍊熶綅
				end
			end else if(next_min == min)begin//鍙兘闇€瑕佸€熶綅
				if(borrow_min)begin
					diff_min <= 59;  // 鍊熶綅
					borrow_hour <= 1;  // 闇€瑕佷粠灏忔椂鍊熶綅
				end else begin
					diff_min <= 0;  // 鍊熶綅
					borrow_hour <= 0;  // 涓嶉渶瑕佷粠灏忔椂鍊熶綅
				end
            end else begin//鏈韩next_min>min锛屼笉闇€瑕佸€熶綅
				if(borrow_min)begin
					diff_min <= next_min - min - 1;
					borrow_hour <= 0;  // 涓嶉渶瑕佸€熶綅
				end else begin
					diff_min <= next_min - min;
					borrow_hour <= 0;  // 涓嶉渶瑕佸€熶綅
				end
            end

            // 灏忔椂宸殑璁＄畻
            if (next_hour < hour) begin//涓€瀹氶渶瑕佸€熶綅
				if(borrow_hour)begin
					diff_hour <= 11 + next_hour - hour;  // 鍊熶綅
				end else begin
					diff_hour <= 12 + next_hour - hour;  // 鍊熶綅
				end
			end else if(next_hour == hour)begin//鍙兘闇€瑕佸€熶綅
				if(borrow_hour)begin
					diff_hour <= 11;  // 鍊熶綅
				end else begin
					diff_hour <= 0;  // 鍊熶綅
				end
            end else begin//鏈韩next_hour>hour锛屼笉闇€瑕佸€熶綅
				if(borrow_hour)begin
					diff_hour <= next_hour - hour - 1;
				end else begin
					diff_hour <= next_hour - hour;
				end
            end
        end
    end
	
	
    // 灏嗗樊鍊兼樉绀哄埌瀵瑰簲鐨勫瘎瀛樺櫒
    always @(posedge clk or negedge rst) begin
        if (!rst) begin
            diff_hour1 <= 0; diff_hour0 <= 0;
            diff_min1 <= 0; diff_min0 <= 0;
            diff_sec1 <= 0; diff_sec0 <= 0;
        end else begin
            // 鏍煎紡鍖栨椂闂村樊鍒版樉绀哄瘎瀛樺櫒
            diff_hour1 <= diff_hour / 10;   // 灏忔椂鐨勯珮浣�
            diff_hour0 <= diff_hour % 10;   // 灏忔椂鐨勪綆浣�
            diff_min1 <= diff_min / 10;     // 鍒嗛挓鐨勯珮浣�
            diff_min0 <= diff_min % 10;     // 鍒嗛挓鐨勪綆浣�
            diff_sec1 <= diff_sec / 10;     // 绉掔殑楂樹綅
            diff_sec0 <= diff_sec % 10;     // 绉掔殑浣庝綅
        end
    end
endmodule
